package com.cinsea.topic.ext;

import com.alibaba.fastjson.JSON;
import com.cinsea.common.Env;
import com.cinsea.common.ExtAction;
import com.cinsea.common.JdbcDao;
import com.cinsea.common.Page;
import com.cinsea.hrm.service.HumanService;
import com.cinsea.interfaces.form.Formdata;
import com.cinsea.interfaces.form.FormdataServiceImpl;
import com.cinsea.interfaces.model.Cell;
import com.cinsea.interfaces.model.Dataset;
import com.cinsea.log.service.LogService;
import com.cinsea.security.util.JdbcUtil;
import com.cinsea.util.DateUtil;
import com.cinsea.util.NumberUtil;
import com.cinsea.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: xf
 * Date: 13-3-27
 * Time: 下午3:15
 */
public class TopicAction extends ExtAction {
    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String action = StringUtil.null2String(request.getParameter("action"));
        JdbcDao jdbcDao = Env.getBean("jdbcDao");
        HumanService humanService = Env.getBean("humanService");
        if (action.equalsIgnoreCase("gettopics")) {
            LogService logService = Env.getBean("logService");
			String dowid = StringUtil.null2String(request.getParameter("dowid"));
            String types = StringUtil.null2String(request.getParameter("types"));
            int pageNo = NumberUtil.string2Int(request.getParameter("pageno"), 1);
            int pageSize = NumberUtil.string2Int(request.getParameter("limit"), 20);
            if (!StringUtil.isEmpty(request.getParameter("start")))
                pageNo = NumberUtil.string2Int(request.getParameter("start"), 0) / pageSize + 1;
            String topicsql="select id,topic,createdatetime,createuserid from cs_topic where deleted!=1 and  dowid = ? and types=? order by modifydatetime desc,createdatetime desc";
            topicsql = JdbcUtil.getPermissionSql(topicsql, "cs_topic");
            Page pageObject = jdbcDao.pagedQuery(topicsql, new Object[]{dowid, types}, pageNo, pageSize);
            List array = new ArrayList();
            if (pageObject.getTotalSize() != 0) {
                List list = (List) pageObject.getResult();
                for (int i = 0; i < list.size(); i++) {
                    Map object = new HashMap();
                    Map topic = (Map) list.get(i);
                    String objname = "<span>" + topic.get("topic") + "</span>";
                    object.put("topic", objname);
                    object.put("processid", topic.get("id"));
                    List<Map> replys = jdbcDao.queryForList("select processid,createuserid,createdatetime from  cs_topicreply where processid=? order by CREATEDATETIME desc", new Object[]{topic.get("id")});
                    object.put("replycount", replys.size());
                    object.put("replytime", "无");
                    object.put("replyuser", "无");
                    if (replys.size() > 0) {
                        Map lastreply = replys.get(0);
                        long m = DateUtil.getMinutesBetween(DateUtil.getCurDateTime(), (String) lastreply.get("createdatetime"));
                        if (m == 0)
                            object.put("pass", "一分钟内");
                        else if (m < 60) {
                            object.put("pass", m + "分钟前");
                        } else if ((int) m / 60 < 24) {
                            object.put("pass", (int) m / 60 + "小时前");
                        } else if ((int) m / (60 * 24) < 7) {
                            object.put("pass", (int) m / (60 * 24) + "天前");
                        } else {
                            object.put("pass", "一周前");
                        }

                        object.put("replytime", lastreply.get("createdatetime"));

                        object.put("replyuser", humanService.getFree((String) lastreply.get("createuserid")).getObjname());
                    } else {
                        long m = DateUtil.getMinutesBetween(DateUtil.getCurDateTime(), (String) topic.get("createdatetime"));
                        if (m == 0)
                            object.put("pass", "一分钟内");
                        else if (m < 60) {
                            object.put("pass", m + "分钟前");
                        } else if ((int) m / 60 < 24) {
                            object.put("pass", (int) m / 60 + "小时前");
                        } else if ((int) m / (60 * 24) < 7) {
                            object.put("pass", (int) m / (60 * 24) + "天前");
                        } else {
                            object.put("pass", "一周前");
                        }

                    }
                    object.put("createtime", topic.get("createdatetime"));
                    String icon = humanService.getFree((String) topic.get("createuserid")).getPhoto();
                    if (StringUtil.isEmpty(icon)) icon = "/images/base/face.gif";
                    else
                        icon = "/filedownload.do?attachid=" + icon;
                    object.put("icon", icon);
                    object.put("creator", "<a href=javascript:onUrl('/form/formprocess.jsp?processid="+(String) topic.get("createuserid")+"','"+humanService.getFree((String) topic.get("createuserid")).getObjname()+"','tab"+(String) topic.get("createuserid")+"')>"+ humanService.getFree((String) topic.get("createuserid")).getObjname()+"</a>" );
                    if (!logService.hasLog((String) topic.get("id"), Env.getRemoteUser().getId()))
                        object.put("isnew", "1");
                    array.add(object);
                }
            }

            Map objectresult = new HashMap();
            objectresult.put("result", array);
            objectresult.put("totalcount", pageObject.getTotalSize());
            response.getWriter().print(JSON.toJSONString(objectresult));
            return;
        }
        if (action.equals("createtopic")) {
            String topiccontent = StringUtil.null2String(request.getParameter("topiccontent"));
            if (StringUtil.isEmpty(topiccontent)) {
                response.getWriter().print("success");
                return;
            }
            FormdataServiceImpl fdsi = new FormdataServiceImpl();
            Formdata ri = new Formdata();
            ri.setOperator(Env.getRemoteUser().getId());
            ri.setTypeid("3");
            Dataset ds = new Dataset();
            List<Cell> mainlist = new ArrayList();
            Cell c1 = new Cell();
            c1.setId("cs_topic_topic");
            c1.setValue(topiccontent);
            mainlist.add(c1);
            Cell c2 = new Cell();
            c2.setId("cs_topic_types");
            c2.setValue("4028df2f3a4d60c2013a4dc3155a02f7");
            mainlist.add(c2);
            ds.setMaintable(mainlist);
            ri.setData(ds);
            fdsi.createFormdata(ri);
            response.getWriter().print("success");

        }
    }
}
